package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.commons.emums.ApplicationName;
import com.atguigu.springcloud.commons.entities.Result;
import com.atguigu.springcloud.entities.Dept;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author horay
 * @category Class description
 * @email horay_hyc@qq.com
 * @date 2018/6/9 20:35
 */
@Slf4j
@RestController
@RequestMapping("/consumer/dept")
public class DeptController {

    private static final String REST_TEMPLATE_PREFIX = "http://" + ApplicationName.MicroservicecloudDept.name;

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/add")
    public Result add(@RequestBody Dept dept) {
        return restTemplate.postForObject(REST_TEMPLATE_PREFIX + "/dept", dept, Result.class);
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody Dept dept) {
        restTemplate.put(REST_TEMPLATE_PREFIX + "/dept", dept);
        return Result.ok();
    }

    @GetMapping("/delete/{deptNo}")
    public Result delete(@PathVariable Long deptNo) {
        restTemplate.delete(REST_TEMPLATE_PREFIX + "/dept/" + deptNo);
        return Result.ok();
    }

    @GetMapping("/get/{deptNo}")
    public Result get(@PathVariable Long deptNo) {
        return restTemplate.getForObject(REST_TEMPLATE_PREFIX + "/dept/" + deptNo, Result.class);
    }

    @GetMapping("/find")
    public Result find() {
        return restTemplate.getForObject(REST_TEMPLATE_PREFIX + "/dept", Result.class);
    }

    @GetMapping("/discovery")
    public Result discovery() {
        return restTemplate.getForObject(REST_TEMPLATE_PREFIX + "/discovery", Result.class);
    }

}
